<?php
class TerritoriesController extends AppController {
    var $name = 'Territories';

    function index() {
        $this->Territory->recursive = 0;
        $this->set('territories', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid territory', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('territory', $this->Territory->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Territory->create();
            if ($this->Territory->save($this->data)) {
                $this->Session->setFlash(__('The territory has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The territory could not be saved. Please, try again.', true));
            }
        }		
        
        $territoryTypes = $this->Territory->TerritoryType->find('list',
                array('conditions' => array('TerritoryType.lookup_type' => 'TERRITORY_TYPE')));

		// Inicialmente esta vacio, se setea cuando elige un territory_type       
        $parentTerritories = array();
        $this->set(compact('parentTerritories', 'territoryTypes'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid territory', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Territory->save($this->data)) {
                $this->Session->setFlash(__('The territory has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The territory could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Territory->read(null, $id);
        }
        $parentTerritories = $this->Territory->ParentTerritory->find('list');
        $territoryTypes = $this->Territory->TerritoryType->find('list');
        $this->set(compact('parentTerritories', 'territoryTypes'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for territory', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->Territory->delete($id)) {
            $this->Session->setFlash(__('Territory deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('Territory was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
    
	function update_select(){

	    if(!empty($this->data['Territory']['territory_type'])) {              
	        $territoryType = $this->data['Territory']['territory_type'];
	
	        if ($territoryType == '25') //PROVINCE
	        {
	            $options = array();
	        }                           
	        
	        if ($territoryType == '26') // CITY
	        {                   
	            $options = $this->Territory->ParentTerritory->find('list',
	            array('conditions' => array('ParentTerritory.territory_type' => 'PROVINCE') ));                              
	        }
	        
	    	if ($territoryType == '27') //NEIGHBORHOOD
	        {                   
	            $options = $this->Territory->ParentTerritory->find('list',
	            array('conditions' => array('ParentTerritory.territory_type' => 'CITY') ));                              
	        }
	      }    

    	$this->set(compact('options'));       
	}
}
?>
